import 'package:flutter/material.dart';
import 'package:jd_app/pages/buyshop.dart';
import 'package:jd_app/pages/catePage.dart';
import 'package:jd_app/pages/homePage.dart';
import 'package:jd_app/pages/userPage.dart';
import 'package:provider/provider.dart';
import 'package:jd_app/provider/current_provider.dart';


class IndexPage extends StatefulWidget {
  const IndexPage({super.key});

  @override
  State<IndexPage> createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> {

  // int currentIndex = 0;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
      // getHomeData();
     CurrentIndexProvider currentIndexProvider = Provider.of<CurrentIndexProvider>(context);
    return Scaffold(
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: '首页',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.category),
            label: '分类',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.shopping_cart),
            label: '购物车',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            label: '我的',
          ),
        ],
        // currentIndex: currentIndex,
        currentIndex: currentIndexProvider.current,
        selectedItemColor: Colors.amber[800],
        onTap: (int index) {
          print(index);
          // setState(() {
          //   currentIndex = index;
          // });
          currentIndexProvider.setCurrent(index);
        },
      ),
      body: IndexedStack(
        // index: currentIndex,
        index: currentIndexProvider.current,
        children: const <Widget>[
          Center(
            child: HomePage(),
          ),
          Center(
            child: CatePage(),
          ),
          Center(
            child: BuyShop(),
          ),
          Center(
            child: UserPage(),
          ),
        ],
      ),
    );
  }
}